package com.ruoyi.project.modules.fieldEnum.service.impl;

import java.util.*;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.modules.fieldEnum.domain.FieldEnumImport;
import com.ruoyi.project.modules.table.domain.FieldInfo;
import com.ruoyi.project.modules.table.mapper.TableInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.modules.fieldEnum.mapper.FieldEnumMapper;
import com.ruoyi.project.modules.fieldEnum.domain.FieldEnum;
import com.ruoyi.project.modules.fieldEnum.service.IFieldEnumService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 字段枚举Service业务层处理
 *
 * @author smallrain
 * @date 2021-12-19
 */
@Service
public class FieldEnumServiceImpl implements IFieldEnumService {

    @Resource
    private FieldEnumMapper fieldEnumMapper;

    @Resource
    private TableInfoMapper tableInfoMapper;

    /**
     * 查询字段枚举
     *
     * @param id 字段枚举主键
     * @return 字段枚举
     */
    @Override
    public FieldEnum selectFieldEnumById(Long id) {
        return fieldEnumMapper.selectFieldEnumById(id);
    }

    /**
     * 查询字段枚举列表
     *
     * @param fieldEnum 字段枚举
     * @return 字段枚举
     */
    @Override
    public List<FieldEnum> selectFieldEnumList(FieldEnum fieldEnum) {
        return fieldEnumMapper.selectFieldEnumList(fieldEnum);
    }

    /**
     * 新增字段枚举
     *
     * @param fieldEnum 字段枚举
     * @return 结果
     */
    @Override
    public int insertFieldEnum(FieldEnum fieldEnum) {
        fieldEnum.setCreateTime(DateUtils.getNowDate());
        return fieldEnumMapper.insertFieldEnum(fieldEnum);
    }

    /**
     * 修改字段枚举
     *
     * @param fieldEnum 字段枚举
     * @return 结果
     */
    @Override
    public int updateFieldEnum(FieldEnum fieldEnum) {
        fieldEnum.setUpdateTime(DateUtils.getNowDate());
        return fieldEnumMapper.updateFieldEnum(fieldEnum);
    }

    /**
     * 批量删除字段枚举
     *
     * @param ids 需要删除的字段枚举主键
     * @return 结果
     */
    @Override
    public int deleteFieldEnumByIds(String ids) {
        return fieldEnumMapper.deleteFieldEnumByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除字段枚举信息
     *
     * @param id 字段枚举主键
     * @return 结果
     */
    @Override
    public int deleteFieldEnumById(Long id) {
        return fieldEnumMapper.deleteFieldEnumById(id);
    }

    @Override
    public List<FieldEnum> fieldList() {
        return this.fieldEnumMapper.fieldList();
    }

    @Override
    @Transactional
    public String importData(List<FieldEnumImport> fieldEnumImportList) {
        Map<String, Long> cache = new HashMap<>();
        Set<String> errTips = new HashSet<>();
        int successNum = 0, errorNum = 0;
        if (CollectionUtil.isNotEmpty(fieldEnumImportList)) {
            for (FieldEnumImport fieldEnumImport : fieldEnumImportList) {
                Long fieldId = cache.get(fieldEnumImport.getTableEnName() + fieldEnumImport.getFieldEnName());
                if (fieldId == null) {
                    FieldInfo fieldInfo = this.tableInfoMapper.selectFieldInfo(fieldEnumImport.getTableEnName(), fieldEnumImport.getFieldEnName());
                    if (fieldInfo != null && fieldInfo.getIsFieldEnum().equals("Y")) {
                        fieldId = fieldInfo.getId();
                        if (fieldId != null) {
                            cache.put(fieldEnumImport.getTableEnName() + fieldEnumImport.getFieldEnName(), fieldId);
                        }
                    }
                }
                if (fieldId != null) {
                    FieldEnum fieldEnum = new FieldEnum();
                    fieldEnum.setFieldId(fieldId);
                    fieldEnum.setEnumName(fieldEnumImport.getEnumName());
                    fieldEnum.setEnumValue(fieldEnumImport.getEnumValue());
                    this.fieldEnumMapper.insertFieldEnum(fieldEnum);
                    successNum ++;
                } else {
                    errTips.add(fieldEnumImport.getTableEnName() + "-"+ fieldEnumImport.getFieldEnName());
                }
            }
            if (errTips.size() > 0) {
                return "导入成功" + successNum + "条, 错误："+errorNum+"条，错误提醒：" + JSONObject.toJSONString(errTips);
            }
            return "导入成功" + successNum + "条";
        }
        return "导入失败";
    }

    @Override
    public List<FieldEnum> selectFieldEnumListByFieldIds(List<Long> fieldIds) {
        return this.fieldEnumMapper.selectFieldEnumListByFieldIds(fieldIds);
    }
}
